﻿CREATE PROCEDURE [dbo].[Articles_InsertFromTempSpaces]
	@RangeReviewId int
AS

INSERT INTO Articles(RangeReviewId, CategoryId, Code, UOM, OrderUOM, Name, EAN, Brand, Manufacturer, Delist, IsDelisted, IsDeletedRangeReview, IsDeletedPerformance, IsNew, TaxRate, Source)
SELECT @RangeReviewId RangeReviewId, c.Id CategoryId, ArticleCode, s.UOM, s.UOM, ArticleName, null, null, null, 0, 1, 1, 0, 0, 0, 'SpacesMaster'
FROM TempSpaces s
Inner Join Categories c on s.MC = c.Code
WHERE ArticleId = 0 OR ArticleId is null
GROUP BY ArticleCode, ArticleName, c.Id, s.UOM

--Hago Update del ArticleId en TempSpaces
Update TempSpaces
Set ArticleId = a.Id
From TempSpaces t
Inner Join Articles a on t.ArticleCode = a.Code and t.UOM = a.UOM and (t.ArticleId = 0 OR t.ArticleId is null)
